# DRF的GenericAPIView、Response
# t拓展类必须配合GenericAPIView 一块使用
from rest_framework.mixins import ListModelMixin, CreateModelMixin, UpdateModelMixin, DestroyModelMixin, \
    RetrieveModelMixin
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from books.serializers import BookModelSerialzier
from books.models import BookInfo


class BooksView(GenericAPIView, ListModelMixin, CreateModelMixin):
    # 指定查询集属性
    queryset = BookInfo.objects.all()
    # 指定序列化器属性
    serializer_class = BookModelSerialzier

    def post(self, request):
        """
           保存图书
       :param request:
       :return:
        """
        return self.create(request)

    def get(self, request):
        """
                获取所有图书
               :param request:
               :return:
        """
        return self.list(request)


class BookView(GenericAPIView, DestroyModelMixin, UpdateModelMixin, RetrieveModelMixin):
    # 指定查询集属性
    queryset = BookInfo.objects.all()
    # 指定序列化器属性
    serializer_class = BookModelSerialzier

    def delete(self, request, pk):
        """
             删除一本图书
         :param request:
         :param pk: 图书id
         :return:
      """
        return self.destroy(request)

    def put(self, request, pk):
        """
            更新一本图书
        :param request:
        :param pk: 图书id
        :return:
        """
        return self.update(request)

    def get(self, request, pk):
        """
            获取一本图书
        :param request:
        :param pk:  图书id
        :return:
        """

        return self.retrieve(request)
